package com.doit.doit47;

import com.alibaba.fastjson.JSON;
import lombok.*;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.RandomUtils;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.common.serialization.StringSerializer;

import java.util.Properties;

public class _08_练习之生产者 {
    public static void main(String[] args) throws InterruptedException {

        Properties props = new Properties();
        props.setProperty(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG,"linux01:9092");
        props.setProperty(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
        props.setProperty(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG,StringSerializer.class.getName());

        KafkaProducer<String, String> producer = new KafkaProducer<String, String>(props);
        EventLog eventLog = new EventLog();
        while (true){
            eventLog.setGuid(RandomUtils.nextInt(1,10000000));
            eventLog.setEventId(RandomStringUtils.randomAlphabetic(10));
            eventLog.setTimestamp(System.currentTimeMillis());
            producer.send(new ProducerRecord<String,String>("event-log", JSON.toJSONString(eventLog)));
            producer.flush();
            Thread.sleep(RandomUtils.nextInt(100,200));
        }



    }
}

//创建一个javaBean
@ToString
@NoArgsConstructor
@AllArgsConstructor
@Setter
@Getter
@Data
class EventLog{
    private int guid;
    private String eventId;
    private long timestamp;
}
